$(function(){
	var admin_id = getCookie('admin_id');
	if(!admin_id){
		window.location.replace('login.html');
	}
	$('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });
    $(".check-all").click(function(){
		$(".ids").prop("checked", this.checked);
	});
	$(".ids").click(function(){
		var option = $(".ids");
		option.each(function(i){
			if(!this.checked){
				$(".check-all").prop("checked", false);
				return false;
			}else{
				$(".check-all").prop("checked", true);
			}
		});
	});
	list_value();
});
function list_value(){
    $.ajax({
		url: ApiUrl + "/order/cart?limit="+Page,
        type: 'get',
        dataType: 'JSON',
        success: function(result) {
        	if(result.code == 200){
        		var html='';
        		$.each(result.data, function(k, v) {
        			html+='<tr>'
        					+'<td><input type="checkbox" class="ids" name="ids[]" value="'+v.id+'"></td>'
                            +'<td>'+v.id+'</td>'
                            +'<td>'+v.zh_name+'（'+v.en_name+'）</td>'
                            +'<td>'+v.price+'</td>'
                            +'<td class="col-sm-1"><input onblur="changenum('+v.id+')" id="counts_'+v.id+'" name="counts" class="form-control" value="'+v.counts+'" /></td>';
                   html+='<td>'+v.created_at+'</td>';
        		});
        		$("#value").html(html);
        		$('.i-checks').iCheck({
			        checkboxClass: 'icheckbox_square-green',
			        radioClass: 'iradio_square-green',
			    });
        	}else{
	    		parent.layer.msg(result.message);
	    		return false;
	    	}
        }
    });
}
function changenum(id){
	var num = $("#counts_"+id).val(); //获取
	 $.ajax({
		url: ApiUrl + "/order/cart/"+id,
	    type: 'post',
	    data:{
	    	counts : num
	    },
	    dataType: 'JSON',
	    success: function(result) {
	    	if(result.code == 200){
	    		if(result.data == 1){
	    			parent.layer.msg("商品数量更新成功!");
	    		}else{
	    			parent.layer.msg("商品数量更新失败!");
	    		}
	    	}else{
	    		parent.layer.msg("商品数量更新失败!");
	    	}
    	}
    });
}
function deleteCart(){
	var gnl=confirm("你真的确定要删除吗?"); 
	if (gnl==true){ 
	    $.ajax({
			url: ApiUrl + "/order/cart/clear",
		    type: 'post',
		    dataType: 'JSON',
		    success: function(result) {
		    	if(result.code == 200){
		    		parent.layer.msg("购物车已清空!");
		    		setTimeout(function(){location.reload();},3000);
		    	}else{
		    		parent.layer.msg("清除购物车失败!");
		    		return false;
		    	}
		    }
	    })
	}else{ 
		return false; 
	} 
}

function deleteGoods(){
	var ids=new Array();
	 $("input[name='ids[]']:checked").each(function () {
             ids.push(this.value);
  });
	var gnl=confirm("你真的确定要删除吗?"); 
	if (gnl==true){ 
	    $.ajax({
			url: ApiUrl + "/order/cart/delete",
		    type: 'post',
		    data:{
		    	ids :ids
		    },
		    dataType: 'JSON',
		    success: function(result) {
		    	if(result.code == 200){
		    		parent.layer.msg("操作成功!");
		    		setTimeout(function(){location.reload();},3000);
		    	}else{
		    		parent.layer.msg("操作失败!");
		    	}
		    }
	    })
	}else{ 
		return false; 
	} 
}

